import requests
import json
import pandas as pd
import time
from datetime import datetime

# res = requests.get(url='http://www.baidu.com/')
# res.encoding = res.apparent_encoding
# # print(res.text)
# print(res.headers)
# # print(res.content)
# cn=导演&ci=0&pcn=娱乐明星&pci=0&ct=1&rn=20&pn=1
# kv = {
#     'cn': '导演',
#     'ci':'0',
#     'pcn': '娱乐明星',
#     'pci': '0',
#     'ct': '1',
#     'rn': '20',
#     'pn': '1'
# }
# # r = requests.request('GET', 'http://tieba.baidu.com/f/index/forumpark', params=kv)
# r = requests.get('http://tieba.baidu.com/f/index/forumpark', params=kv)
# r.encoding = r.apparent_encoding
# print(r.text)

# 京东商品页面获取
# kv = {
#     'keyword': '电脑'
# }
#
# headers = {
#     'Cookie': '__jdu=1629253722; areaId=14; shshshfpa=532f479d-9f40-94ed-ca93-a762a222cf10-1705287059; shshshfpx=532f479d-9f40-94ed-ca93-a762a222cf10-1705287059; TrackID=1fB3jv-Ny7UfHkVPNtthdpNUzhUcDSNHLpYI6HnVcXYKVoXdZY2fmNTwEvY8iQddSfsrxA0BwpBAuHsZ_K_Scnqjho7cMtfMapqLqvBl6t1Q; thor=DAF3A4A791EF83D5BAEE45EEDA9FFA0DDB87A069538270D37F395C6648A99ABB6BA661623507756FA4D063066C0C2652A7CC00D6BD8D1D2B50ABCB637B9057FF60A54424C395A90D6275F9DB69897E1E6266CF6D7EF9E265E7B6B741A94ADA013E005444F2493B18B631EDB0582CE84A3B5F4C9C2513193EE144BC6AF253EFC47C6CC6226DB79984C179FCADE989F1B1D53C6864648ABAFB366983F8AB1C62FD; flash=2_uLSf7di-_CUG_uij68KWOHDjQ7zunP_9RfJ9LQySLhiIjR7fwioffSXAQJkEJLUJyZhRwouEGZjxWtzfU0nSiRqyhJ5j1mCoYzTFs7p5aBP*; pinId=vNXG4Ha0Ua9gimv_JfPM57V9-x-f3wj7; pin=jd_51b13ba7a04fe; unick=%E4%BE%9D%E7%AC%91%E4%B8%8D%E6%9F%93; _tp=wMnG0gOlkC9pAasZ1MHUc3ecV3%2FhpB2V6A3lol%2BnZcE%3D; _pst=jd_51b13ba7a04fe; user-key=a00e9fa3-7a45-46b2-a9f4-1046b481e1e2; ipLoc-djd=14-1116-3431-57939; PCSYCityID=CN_340000_340100_0; qrsc=3; unpl=JF8EALBnNSttCENdAx9SEkFEH1lUWw1fS0QCa24GAVheTFADSQEdEUd7XlVdWBRKFx9vbhRUXFNJUg4bAisSEXteXVdZDEsWC2tXVgQFDQ8VXURJQlZAFDNVCV9dSRZRZjJWBFtdT1xWSAYYRRMfDlAKDlhCR1FpMjVkXlh7VAQrAhwRGEhZVlheC3sWM2hXNWReUUtTBR0yGiIRex8AAl4ISBIFbSoFU15QSFAHHQEYIhF7Xg; __jdv=76161171|baidu-pinzhuan|t_288551095_baidupinzhuan|cpc|0f3d30c8dba7459bb52f2eb5eba8ac7d_0_a9875a8bee514af1b0582d47657c272f|1705545434319; 3AB9D23F7A4B3CSS=jdd034A7YHVOHI3IQFUSHOZBZ76OZ6TECLHQ64S3VD3M4SY7GLRFK7CJN75OIOT5KY6SDCZ2Q2ZHQ4IDSUN3LVNVGRMKBD4AAAAMNDJW7WWYAAAAADFLS5O7D6C5AMYX; _gia_d=1; joyya=1705545436.1705545437.22.1j73e89; jsavif=1; jsavif=1; xapieid=jdd034A7YHVOHI3IQFUSHOZBZ76OZ6TECLHQ64S3VD3M4SY7GLRFK7CJN75OIOT5KY6SDCZ2Q2ZHQ4IDSUN3LVNVGRMKBD4AAAAMNDJW7WWYAAAAADFLS5O7D6C5AMYX; __jda=143920055.1629253722.1705287057.1705396781.1705545434.3; __jdc=143920055; rkv=1.0; avif=1; __jdb=143920055.4.1629253722|3.1705545434; shshshsID=26f96ff3aa8f6af0fb488f06d1f13198_2_1705545451315; shshshfpb=BApXeV6xmGehA2emNbQd6WXhazhUZ5H7KBkdpd3Zj9xJ1MjOmuYO2; 3AB9D23F7A4B3C9B=4A7YHVOHI3IQFUSHOZBZ76OZ6TECLHQ64S3VD3M4SY7GLRFK7CJN75OIOT5KY6SDCZ2Q2ZHQ4IDSUN3LVNVGRMKBD4',
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
# }
# r = requests.get(url='http://search.jd.com/Search', params=kv, headers=headers)
# r.encoding = r.apparent_encoding
# print(r.text)

# 猫眼数据
url = 'https://piaofang.maoyan.com/dashboard-ajax?orderType=0&uuid=18d1a320d2ac8-0176ff6be08f1-26001951-1fa400-18d1a320d2ac8&timeStamp=1705541514892&User-Agent=TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzEyMC4wLjAuMCBTYWZhcmkvNTM3LjM2&index=301&channelId=40009&sVersion=2&signKey=596012c79eabec24a627610c90a3718c'
headers = {
    'Cookie': '_lxsdk_cuid=18d1a320d2ac8-0176ff6be08f1-26001951-1fa400-18d1a320d2ac8; _lxsdk=18d1a320d2ac8-0176ff6be08f1-26001951-1fa400-18d1a320d2ac8; _lxsdk_s=18d1a320d2a-cc3-d5d-689%7C%7C2; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic',
    'Host': 'piaofang.maoyan.com',
    'Referer': 'https://piaofang.maoyan.com/dashboard',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}
res = requests.get(url=url, headers=headers)
res.encoding = res.apparent_encoding
# print(res.text)
dict1 = json.loads(res.text)
# print(dict1)
web_list = dict1['webList']['data']['list']
# 创建对应值的列表
rd_list = []  # 热度
name_list = []  # 剧名
qd_list = []  # 渠道
times_list = []  # 累计上线时间

for web in web_list:
    rd = web['currHeatDesc']
    rd_list.append(rd)
    infos = web['seriesInfo']
    name = infos['name']
    name_list.append(name)
    qd = infos['platformDesc']
    qd_list.append(qd)
    times = infos['releaseInfo']
    times_list.append(times)
    print(f'剧名:{name}，正在{qd}热播, 累计上线{times}, 当前热度为:{rd}')
    print('-----------------------------')

# 将列表对应值封装字典
info_dict1 = {
    '剧名': name_list,
    '渠道': qd_list,
    '累计上线时间': times_list,
    '热度': rd_list
}

# 将字典转DataFrame
df1 = pd.DataFrame(info_dict1)
day_time = datetime.strftime(datetime.now(), '%Y-%m-%d')
df1.to_csv(f'data/猫眼网播热度剧-{day_time}.csv', index=False)
